import requests
import json

# Label Studio 配置
api_url = "http://localhost:8080/api/projects/3/export?exportType=JSON"
headers = {
    "Cookie": "_ga=GA1.1.1453332884.1739757874; _gid=GA1.1.939860068.1739757874; csrftoken=zkz3YzoqG07mlF6s3wLMOqXeb5S1C7Mc; _ga_Z4KXEBY4VP=GS1.1.1739757874.1.1.1739757892.42.0.0; sessionid=.eJxVj8luhDAQRP_F5wF5aW8cc883oDZtgzPIHmGQsij_HojmMrfurqpX6h92ZGIDQyMBI7jOY8IOrPadAw0dahSRjEkOkN3Yim0f1zrnwgZhlbfaOi97KQGUUTc24rEv49HiNv5jBXu5BZzusVwCfWCZaz_Vsm859Jelf6qtf68U17en9wWwYFvONFdTAiPI0jlhdBpITIFSkga4ShZCCip5MxkXgiD03inOE-fGaiIfTmjdZiz5G_dcy_i4n--cRS22du3x85G3LzaAkpzz3z-ailv3:1tjsic:kn_AQWyEgiMNP8RCTtJ59ZXA5UxDTPNYgtqlh3zo3nU"
}

response = requests.get(api_url, headers=headers)
data = response.json()
print(data)
# 自定义格式化处理
custom_data = []

for item in data:
    annotations = item["annotations"]
    for annotation in annotations:
        for result in annotation.get("result", []):
            custom_item = {
                "label": [result['value']['labels'][0]],
                "value": {
                    "start": result['value']['start'],
                    "end": result['value']['end'],
                    "text": result['value']['text']
                }
            }
            custom_data.append(custom_item)

# 将结果保存为 JSON 文件
with open('custom_export.json', 'w') as f:
    json.dump(custom_data, f, separators=(',', ':'), ensure_ascii=False)
